//
//  GoogleProduct.m
//  Product Hunter
//
//  Created by Han Wang on 11/13/11.
//  Copyright (c) 2011 The Masters. All rights reserved.
//

#import "GoogleProduct.h"

#define APIKEY @"AIzaSyCUn0MYf6psJk-dn1WNVTjh7B_YJWSlUPE"

@implementation GoogleProduct

@synthesize tableView;

#pragma mark -
#pragma mark Instance methods

- (void)updateData
{
    [self.tableView reloadData];
}

// Method to get a product from a Google Shopping product search.
- (GTLShoppingModelProduct *)getProduct:(GTLShoppingProduct *)item
{
    return item.product;
}

// Method to get the title of a product from a Google Shopping product search.
- (NSString *)getProductTitle:(GTLShoppingProduct *)item
{
    return [self getProduct:item].title;
}

// Method to get the product page link of a product from a Google Shopping product search.
- (NSString *)getProductLink:(GTLShoppingProduct *)item
{
    return [self getProduct:item].link;
}

// Method to get the image of a product from a Google Shopping product search.
- (GTLShoppingModelProductImagesItem *)getProductImage:(GTLShoppingProduct *)item
{
    return [[self getProduct:item].images objectAtIndex:0];
}

// Method to get the thumbnail of a product from a Google Shopping product search.
- (GTLShoppingModelProductImagesItemThumbnailsItem *)getProductImageThumbnail:(GTLShoppingProduct *)item
{
    return [[self getProductImage:item].thumbnails objectAtIndex:0];
}

// Method to get the link of the thumbnail of a product from a Google Shopping product search.
- (NSString *)getProductThumbNailLink:(GTLShoppingProduct *)item 
{
    return [self getProductImageThumbnail:item].link;
}

// Method to get the inventory of a product from a Google Shopping product search.
- (GTLShoppingModelProductInventoriesItem *)getProductInventory:(GTLShoppingProduct *)item
{
    return [[self getProduct:item].inventories objectAtIndex:0];
}

// Method to get the price of a product from a Google Shopping product search.
- (NSNumber *)getProductPrice:(GTLShoppingProduct *)item 
{
    return [self getProductInventory:item].price;
}

// Method to get the retailer of a product from a Google Shopping product search.
- (NSString *)getProductRetailerName:(GTLShoppingProduct *)item
{
    return [[self getProduct:item].author valueForKey:@"name"];
}

// Method to construct a Google Shopping product search query.
- (GTLQueryShopping *)makeShoppingQuery:(NSString *)item :(NSString *)rankBy :(NSString *)thumbnailSize :(NSString *)country :(int)maxResults
{
    GTLQueryShopping *query = [GTLQueryShopping queryForProductsListWithSource:@"public"];
    query.q = item;
    query.rankBy = rankBy;
    query.thumbnails = thumbnailSize;
    query.country = country;
    query.maxResults = maxResults;
    return query;
}

// Method to search products using Google Shopping product search.
- (void)searchProduct:(NSString *)searchItem storedAt:(NSMutableArray *)products
{
    [self updateData];
    
    GTLServiceShopping *service = [[GTLServiceShopping alloc] init];
    service.APIKey = APIKEY;
    
    GTLQueryShopping *query = [self makeShoppingQuery:searchItem :@"relevancy" :@"56:56" :@"US" :18];

    GTLServiceTicket *productsTicket = 
        [service executeQuery:query completionHandler:^(GTLServiceTicket *ticket, id object, NSError *error) {
        if (error == nil) {
            GTLShoppingProducts *googleProducts = object;
            for (GTLShoppingProduct *item in googleProducts) {
                NSMutableDictionary *product = [[NSMutableDictionary alloc] initWithCapacity:1];
                [product setObject:[self getProductTitle:item] forKey:@"title"];
                [product setObject:[self getProductLink:item] forKey:@"link"];
                [product setObject:[self getProductPrice:item] forKey:@"price"];
                [product setObject:[self getProductThumbNailLink:item] forKey:@"thumbnail"];
                [product setObject:[self getProductRetailerName:item] forKey:@"retailer"];
                NSLog(@"%@", product);
                [products addObject:product];
                googleProducts = nil;
                [product release];
            }
        }
    [self updateData];
    }];
    service = nil;
    query = nil;
    productsTicket = nil;
}

@end
